<?php
session_start();
if(!isset($_SESSION['admin'])) {
    header('Location: login.php');
    exit;
}

include_once 'includes/db.php';
include_once 'includes/functions.php';

// 获取统计数据
$total_products = getTotalProducts();
$total_applications = getTotalApplications();
$total_contacts = getTotalContacts();
$total_news = getTotalNews();
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>优塑科技 - 后台管理系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="assets/css/admin.css">
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
        <div class="container-fluid">
            <a class="navbar-brand" href="index.php">优塑科技后台</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="logout.php">退出登录</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <nav class="col-md-3 col-lg-2 d-md-block bg-light sidebar">
                <div class="position-sticky pt-3">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link active" href="index.php">
                                <i class="bi bi-speedometer2 me-2"></i>控制台
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="products.php">
                                <i class="bi bi-box me-2"></i>产品管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="applications.php">
                                <i class="bi bi-grid me-2"></i>应用领域
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="news.php">
                                <i class="bi bi-newspaper me-2"></i>新闻管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="contacts.php">
                                <i class="bi bi-envelope me-2"></i>客户咨询
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="settings.php">
                                <i class="bi bi-gear me-2"></i>系统设置
                            </a>
                        </li>
                    </ul>
                </div>
            </nav>

            <!-- 主要内容区 -->
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">控制台</h1>
                </div>

                <!-- 统计卡片 -->
                <div class="row mb-4">
                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-primary shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                            产品总数
                                        </div>
                                        <div class="h5 mb-0 font-weight-bold text-gray-800">
                                            <?php echo $total_products; ?>
                                        </div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="bi bi-box fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-success shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                            应用领域
                                        </div>
                                        <div class="h5 mb-0 font-weight-bold text-gray-800">
                                            <?php echo $total_applications; ?>
                                        </div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="bi bi-grid fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-info shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                                            客户咨询
                                        </div>
                                        <div class="h5 mb-0 font-weight-bold text-gray-800">
                                            <?php echo $total_contacts; ?>
                                        </div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="bi bi-envelope fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-3 col-md-6 mb-4">
                        <div class="card border-left-warning shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                            新闻文章
                                        </div>
                                        <div class="h5 mb-0 font-weight-bold text-gray-800">
                                            <?php echo $total_news; ?>
                                        </div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="bi bi-newspaper fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 最近客户咨询 -->
                <div class="row">
                    <div class="col-lg-6 mb-4">
                        <div class="card shadow">
                            <div class="card-header py-3">
                                <h6 class="m-0 font-weight-bold text-primary">最近客户咨询</h6>
                            </div>
                            <div class="card-body">
                                <?php
                                $recent_contacts = getRecentContacts(5);
                                if(count($recent_contacts) > 0) {
                                    foreach($recent_contacts as $contact) {
                                        echo '<div class="mb-3">';
                                        echo '<div class="small text-gray-500">' . date('Y-m-d H:i', strtotime($contact['created_at'])) . '</div>';
                                        echo '<div class="font-weight-bold">' . htmlspecialchars($contact['name']) . '</div>';
                                        echo '<div class="small">' . htmlspecialchars($contact['email']) . '</div>';
                                        echo '<div class="small">' . htmlspecialchars(substr($contact['message'], 0, 100)) . '...</div>';
                                        echo '</div>';
                                    }
                                } else {
                                    echo '<p>暂无客户咨询</p>';
                                }
                                ?>
                                <div class="text-end mt-3">
                                    <a href="contacts.php" class="btn btn-primary btn-sm">查看全部</a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 最近新闻 -->
                    <div class="col-lg-6 mb-4">
                        <div class="card shadow">
                            <div class="card-header py-3">
                                <h6 class="m-0 font-weight-bold text-primary">最近新闻</h6>
                            </div>
                            <div class="card-body">
                                <?php
                                $recent_news = getRecentNews(5);
                                if(count($recent_news) > 0) {
                                    foreach($recent_news as $news) {
                                        echo '<div class="mb-3">';
                                        echo '<div class="small text-gray-500">' . date('Y-m-d', strtotime($news['created_at'])) . '</div>';
                                        echo '<div class="font-weight-bold">' . htmlspecialchars($news['title']) . '</div>';
                                        echo '<div class="small">' . htmlspecialchars(substr($news['content'], 0, 100)) . '...</div>';
                                        echo '</div>';
                                    }
                                } else {
                                    echo '<p>暂无新闻</p>';
                                }
                                ?>
                                <div class="text-end mt-3">
                                    <a href="news.php" class="btn btn-primary btn-sm">管理新闻</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/admin.js"></script>
</body>
</html>